<?php

namespace app\common\traits;

use think\Route;

trait LinkEncryption
{
    public function setSourceAttr($value)
    {
        if ($value) {
            $value = encrypt($value);
        }
        return $value;
    }

    public function setVideoAttr($value)
    {
        if ($value) {
            $value = encrypt($value);
        }
        return $value;
    }

    public function getVideoAttr($value)
    {
        return $this->_getOriginalByModule($value);
    }

    public function getSourceAttr($value)
    {
        return $this->_getOriginalByModule($value);
    }

    public function setVoiceAttr($value)
    {
        if ($value) {
            $value = encrypt($value);
        }
        return $value;
    }

    public function getVoiceAttr($value)
    {
        return $this->_getOriginalByModule($value);
    }

    public function setPronounceAttr($value)
    {
        if ($value) {
            $value = encrypt($value);
        }
        return $value;
    }

    public function getPronounceAttr($value)
    {
        return $this->_getOriginalByModule($value);
    }

    private function _getOriginalByModule($value)
    {
        if ($value) {
            $module = strtolower(request()->module());
            if ('admin' == $module) {
                //如果是管理模块,那么就解密
                try {
                    $value = decrypt($value);
                } catch (\Throwable $exception) {
                }
            }
        }
        return $value;
    }
}